package com.example.calculator;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
//import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.text.InputType;
import android.view.Menu;
//import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.lang.Math;

public class MainActivity extends Activity 
{
	
    protected double a,b;
    protected int flag;
    protected String x;

    
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
    	final EditText txtNhap = (EditText)findViewById(R.id.txtNhap);
    	final Button btn_num_0 = (Button)findViewById(R.id.btn_num_0);
    	final Button btn_num_1 = (Button)findViewById(R.id.btn_num_1);
    	final Button btn_num_2 = (Button)findViewById(R.id.btn_num_2);
    	final Button btn_num_3 = (Button)findViewById(R.id.btn_num_3);
    	final Button btn_num_4 = (Button)findViewById(R.id.btn_num_4);
    	final Button btn_num_5 = (Button)findViewById(R.id.btn_num_5);
    	final Button btn_num_6 = (Button)findViewById(R.id.btn_num_6);
    	final Button btn_num_7 = (Button)findViewById(R.id.btn_num_7);
    	final Button btn_num_8 = (Button)findViewById(R.id.btn_num_8);
    	final Button btn_num_9 = (Button)findViewById(R.id.btn_num_9);
    	final Button btn_bang = (Button)findViewById(R.id.btn_bang);
    	final Button btn_cong = (Button)findViewById(R.id.btn_cong);
    	final Button btn_tru = (Button)findViewById(R.id.btn_tru);
    	final Button btn_nhan = (Button)findViewById(R.id.btn_nhan);
    	final Button btn_chia = (Button)findViewById(R.id.btn_chia);
    	final Button btn_dot = (Button)findViewById(R.id.btn_dot);    
    	final Button btn_ac = (Button)findViewById(R.id.btn_AC);
    	final Button btn_canbachai = (Button)findViewById(R.id.btn_canbachai);
    	final TextView lblPhepTinh = (TextView)findViewById(R.id.lblPhepTinh);

    	
    	//double a = 0, b = 0;
    	//char x;
    	
    	/*OnClickListener click_num = new OnClickListener() 
    	{
			
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				String str = txtNhap.getText().toString();
				txtNhap.setText(txtNhap.getText() + btn_num_0.getText().toString());
			}
		};*/
    	
    	//txtNhap.setRawInputType(type)
    	
    	//InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    	//imm.hideSoftInputFromInputMethod(txtNhap.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
    	nhapSo(btn_num_0,lblPhepTinh);
    	nhapSo(btn_num_1,lblPhepTinh);
    	nhapSo(btn_num_2,lblPhepTinh);
    	nhapSo(btn_num_3,lblPhepTinh);
    	nhapSo(btn_num_4,lblPhepTinh);
    	nhapSo(btn_num_5,lblPhepTinh);
    	nhapSo(btn_num_6,lblPhepTinh);
    	nhapSo(btn_num_7,lblPhepTinh);
    	nhapSo(btn_num_8,lblPhepTinh);
    	nhapSo(btn_num_9,lblPhepTinh);
    	nhapSo(btn_dot,lblPhepTinh);
    	phepToan(btn_cong,lblPhepTinh);
    	phepToan(btn_tru,lblPhepTinh);
    	phepToan(btn_nhan,lblPhepTinh);
    	phepToan(btn_chia,lblPhepTinh);
    	//phepToan(btn_canbachai);
    	tinhToan(btn_bang,lblPhepTinh);
    	
    	canBacHai(btn_canbachai,lblPhepTinh);

    	OnClickListener clickAC = new OnClickListener() 
    	{
			
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				txtNhap.setText("");
				a=0;
				b=0;
				x="";
				lblPhepTinh.setText("");
			}
		};
    	btn_ac.setOnClickListener(clickAC);
		//btn_num_0.setOnClickListener(buttonClickListener);
    	
    }
	
	public void canBacHai(final Button btn,final TextView lbl)
	{
		OnClickListener clicksqrt = new OnClickListener() 
		{
			
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				final EditText txtNhap = (EditText)findViewById(R.id.txtNhap);
				//txtNhap.setText(btn.getText().toString());
				double m=0;
				lbl.setText(btn.getText().toString() + "(" + txtNhap.getText().toString() + ")" + "=" );
				m = Double.parseDouble(txtNhap.getText().toString());
				m = Math.sqrt(m);
				txtNhap.setText(Double.toString(m));
				flag = 1;
			}
		};
		btn.setOnClickListener(clicksqrt);
		
	}
	public void tinhToan(final Button btn,final TextView lbl)
	{
		OnClickListener clickBang = new OnClickListener()
		{
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final EditText txtNhap = (EditText)findViewById(R.id.txtNhap);
				b = Double.parseDouble(txtNhap.getText().toString());
				if(x.equals("+"))
				{
					//double c = a+b;
					txtNhap.setText(Double.toString(a+b));
				}
				if(x.equals("-"))
				{
					//double c = a+b;
					txtNhap.setText(Double.toString(a-b));
				}
				if(x.equals("*"))
				{
					//double c = a+b;
					txtNhap.setText(Double.toString(a*b));
				}
				if(x.equals("/"))
				{
					//double c = a+b;
					txtNhap.setText(Double.toString(a/b));
				}
				lbl.setText(lbl.getText() + btn.getText().toString());
				/*if(x.equals("Sqrt"))
				{
					//double c = a+b;
					txtNhap.setText(Double.toString(Math.sqrt(a)));
				}*/

				x="";
				b=0;
				a=0;
				flag = 1;
				//lbl.setText("");
			}
		};
		btn.setOnClickListener(clickBang);
	}
	
    public void phepToan(final Button btn, final TextView lbl)
    {
    	
    	OnClickListener btnclick = new OnClickListener() 
    	{
			
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				final EditText txtNhap = (EditText)findViewById(R.id.txtNhap);
				if(txtNhap.getText().toString().equals(""))
				{
					return;
				}
				if(flag == 1)
				{
					//txtNhap.setText("");
					lbl.setText(txtNhap.getText().toString());
					flag  = 0;
				}
				a = Double.parseDouble(txtNhap.getText().toString());
				txtNhap.setText(btn.getText().toString());
				x = btn.getText().toString() ;
				txtNhap.setText("");
				lbl.setText(lbl.getText() + btn.getText().toString());
			}
		};
		btn.setOnClickListener(btnclick);
    }
    
    public void nhapSo(final Button btn,final TextView lbl)
    {
    	OnClickListener buttonClickListener = new OnClickListener() 
    		{
    		
				public void onClick(View v) 
				{
					// TODO Auto-generated method stub
					final EditText txtNhap = (EditText)findViewById(R.id.txtNhap);
					if(flag == 1)
					{
						txtNhap.setText("");
						lbl.setText("");
						flag  = 0;
					}
					txtNhap.setText(txtNhap.getText() + btn.getText().toString());
					lbl.setText(lbl.getText() + btn.getText().toString());
				}
    		};
    		btn.setOnClickListener(buttonClickListener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
